package com.jeanbart.util;

import com.jeanbart.bean.Player;
import com.jeanbart.service.ClientService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReadReplyFile {

    public ReadReplyFile() {
    }

    public List<List<Player>>  startRead(boolean serviceState) throws IOException {
        AgetB getInfo = new AgetB();
        //获取config.json信息
        String JSONFile = getInfo.getPath();
        String service = getInfo.getService();
        File test = new File(JSONFile);
        //tempArenaInfo.json文件不存在，表示游戏未开始
        if (!test.exists()) {
            Player p = new Player();
            p.setAccountName("游戏未开始或者路径错误");
            p.setRate(-1);
            List<Player> p1 = new ArrayList<>();
            List<Player> p2 = new ArrayList<>();
            List<List<Player>> pll = new ArrayList<>();
            p1.add(p);
            p2.add(p);
            pll.add(p1);
            pll.add(p2);
            return pll;
        }
        long fileTime = new File(JSONFile).lastModified();
        File output = new File(getInfo.getConfigFile().getParent() + "//temp");
        //temp文件夹不存在则创建一个
        if (!output.exists())
            output.mkdirs();
        output = new File(output.toString() + "//" + fileTime + ".json");
        if (output.exists()) {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(output));
            List<List<Player>> end = null;
            try {
                end = (List<List<Player>>) ois.readObject();
                ois.close();
                return end;
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
                ois.close();
                output.delete();
            }
        }
        FileReader fir = new FileReader(JSONFile);
        BufferedReader br = new BufferedReader(fir);
        String s = "";
        while (true){
            try{
                s = br.readLine();
                if (s.indexOf('{') == -1)
                    continue;
                else
                    break;
            }catch (Exception e){
                break;
            }
        }
        s = s.substring(s.indexOf('{'), s.lastIndexOf('}') + 1);
        br.close();
        fir.close();
        JSONObject fileJSON = JSONObject.fromObject(s);
        JSONArray ja = fileJSON.getJSONArray("vehicles");//该场所有玩家id
        JSONObject sendJSON = new JSONObject();
        sendJSON.put("list",ja);
        sendJSON.put("service",service);

        List<List<Player>> list = new ClientService(sendJSON,false).SearchResult();
        List<Player> Teammate = list.get(0);
        List<Player> Enemy = list.get(1);
        List<List<Player>> end=new ArrayList<>();
        end.add(Teammate);
        end.add(Enemy);
        output.createNewFile();
        ObjectOutputStream osw=new ObjectOutputStream(new FileOutputStream(output));
        osw.writeObject(end);
        osw.close();
        return end;
    }

    /*public String ServiceState(String service) {
        String requestUrl = "http://106.12.129.176:4396/test";
        Map params = new HashMap();
        params.put("service",service);
        try {
            String string = new WowsHttpRequest().httpRequest(requestUrl, params, 2000);
            JSONObject jo = JSONObject.fromObject(string);
            String s = "";
            s += "次数统计-CN:" + jo.getString("CN")+ "  ";
            s += "EU:" + jo.getString("EU")+ "  ";
            s += "RU:" + jo.getString("RU")+ "  ";
            s += "NA:" + jo.getString("NA")+ "  ";
            s += "ASIA:" + jo.getString("ASIA")+ "  ";
            return s;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "500";
    }*/
}
